package May._06.test30;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;


import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.stream.Collectors;

public class Main {
    public static void main(String[] args) {
        Student[] students = new Student[5];
        inputStudent(students);
        //创建输出流
        OutputStreamWriter out = null;
        try {
            out = new OutputStreamWriter(new FileOutputStream("fileTest\\a.txt", true));
            //统计湖北
            findHuBei(out, students);
            //找出年龄大于18
            findAge18(students, out);
            //统计Z&&18
            containZ18(students,out);
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }finally {
            if (out!=null){
                try {
                    out.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }



    }

    private static void containZ18(Student[] students, OutputStreamWriter out) {
        List<Student> containZ18 = Arrays.stream(students)
                .filter(new Predicate<Student>() {
                    @Override
                    public boolean test(Student student) {
                        return student.name.contains("z") && student.age > 18;
                    }
                })
                .collect(Collectors.toList());
        containZ18.forEach(new Consumer<Student>() {
            @Override
            public void accept(Student student) {
                try {
                    out.write(student.toString());
                    out.write(System.lineSeparator());
                    out.flush();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        });
    }

    private static void findAge18(Student[] students, OutputStreamWriter out) {
        List<Student> age18 = Arrays.stream(students)
                .filter(new Predicate<Student>() {
                    @Override
                    public boolean test(Student student) {
                        return student.age > 18;
                    }
                })
                .collect(Collectors.toList());

        age18.forEach(new Consumer<Student>() {
            @Override
            public void accept(Student student) {
                try {
                    out.write(student.toString());
                    out.write(System.lineSeparator());
                    out.flush();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        });

    }

    private static void findHuBei(OutputStreamWriter out, Student[] students) {
        try {
            //统计湖北
            List<Student> HuBei = Arrays.stream(students)
                    .filter(new Predicate<Student>() {
                        @Override
                        public boolean test(Student student) {
                            return "湖北".equals(student.address);
                        }
                    })
                    .collect(Collectors.toList());

            out.write(HuBei.toString());
            out.write(System.lineSeparator());
            out.flush();
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    private static void inputStudent(Student[] students) {
        Scanner scanner = new Scanner(System.in);
        for (int i = 0; i < students.length; i++) {
            String msg = scanner.nextLine();
            if ("exit".equals(msg)) {
                break;
            }
            String[] split = msg.split(" ");
            int id = Integer.parseInt(split[0]);
            int age = Integer.parseInt(split[3]);
            Student student = new Student(id, split[1], split[2], age);
            students[i] = student;
        }
    }
}

@Data
@AllArgsConstructor
@NoArgsConstructor
class Student {
    int id;
    String name;
    String address;
    int age;
}
